package com.ruoyi.project.battle.battle.controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.battle.battle.domain.BattleUser;
import com.ruoyi.project.battle.battle.service.IBattleUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 战场用户信息管理Controller
 * 
 * @author ruoyi
 * @date 2021-09-22
 */
@Controller
@RequestMapping("/battle/battle")
public class BattleUserController extends BaseController
{
    private String prefix = "battle/battle";

    @Autowired
    private IBattleUserService battleUserService;

    @Autowired
    IRoleService roleService;

/*    @RequiresPermissions("battle:battle:view")*/
    @GetMapping()
    public String battle()
    {
        return prefix + "/battle";
    }


 /*   @PostMapping("/list")
    @ResponseBody
    public String  list1( HttpSession session, HttpServletResponse response) throws IOException {
        Object id = session.getAttribute("userId");
        System.out.println(id);
        response.setContentType("text/html;charset=utf-8");
        Writer out= new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));

     *//*   startPage();
        List<BattleUser> list = battleUserService.selectBattleUserList(battleUser);*//*
        return out.toString();
    }
*/

    /**
     * 查询战场用户信息管理列表
     */
  @RequiresPermissions("battle:battle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BattleUser battleUser, HttpSession session, HttpServletResponse response) throws IOException {
        startPage();
        List<BattleUser> list = battleUserService.selectBattleUserList(battleUser);
        return getDataTable(list);
    }

    @PostMapping("/listuser/{roleId}")
    @ResponseBody
    public TableDataInfo listRoleId(@PathVariable(required = true) Long roleId,BattleUser battleUser)
    {
        startPage();
        battleUser.setRoleId(roleId);
        List<BattleUser> list = battleUserService.selectBattleUserList(battleUser);
        return getDataTable(list);
    }


    /**
     * 导出战场用户信息管理列表
     */
   /* @RequiresPermissions("battle:battle:export")*/
    @Log(title = "战场用户信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BattleUser battleUser)
    {
        List<BattleUser> list = battleUserService.selectBattleUserList(battleUser);
        ExcelUtil<BattleUser> util = new ExcelUtil<BattleUser>(BattleUser.class);
        return util.exportExcel(list, "战场用户信息管理数据");
    }

    /**
     * 新增战场用户信息管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Role> roles = roleService.selectRoleAll();
        mmap.put("roleList", roles);
        return prefix + "/add";
    }

    /**
     * 新增保存战场用户信息管理
     */
  /*  @RequiresPermissions("battle:battle:add")*/
    @Log(title = "战场用户信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BattleUser battleUser)
    {


        return toAjax(battleUserService.insertBattleUser(battleUser));
    }

    /**
     * 修改战场用户信息管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BattleUser battleUser = battleUserService.selectBattleUserById(id);
        List<Role> roles = roleService.selectRoleAll();
        battleUser.setRoleList(roles);
        mmap.put("battleUser", battleUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存战场用户信息管理
     */
 /*   @RequiresPermissions("battle:battle:edit")*/
    @Log(title = "战场用户信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BattleUser battleUser)
    {
        return toAjax(battleUserService.updateBattleUser(battleUser));
    }

    /**
     * 删除战场用户信息管理f
     */
    /*@RequiresPermissions("battle:battle:remove")*/
    @Log(title = "战场用户信息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(battleUserService.deleteBattleUserByIds(ids));
    }
}
